package com.jstyle.jclifexd.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.GrowthMianRecordActivity;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.RankData;
import com.jstyle.jclifexd.model.Teamperformance;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ResolveData;
import com.jstyle.jclifexd.view.goodview.GoodView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RankactivityAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    Context context;
    List<RankData.DataBean> mDataList;
    GoodView mGoodView;
    String getActId;
    public RankactivityAdapter(Context mycontext,String ID) {
        this.context = mycontext;
        this.mDataList = new ArrayList<>();
        this.getActId=ID;
        mGoodView = new GoodView(context);

    }

    public void updateData(List<RankData.DataBean>list){
        this.mDataList=list;
        notifyDataSetChanged();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final  RankData.DataBean rankData=  mDataList.get(position);
        final RankViewHolder rankViewHolder=(RankViewHolder)holder;
        rankViewHolder.tvRankLevel.setVisibility(position<3?View.INVISIBLE:View.VISIBLE);
        rankViewHolder.tvRankLevel.setText(String.valueOf(position+1));
        int textColor=0;
        if(position<3){
            int id =context. getResources().getIdentifier("rank_" + (position+1), "drawable", context.getPackageName());
            rankViewHolder.itemView.setBackgroundResource(id);
            textColor=Color.WHITE;
        }else{
            rankViewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
            textColor=context.getResources().getColor(R.color.rank_text_color);
        }
        rankViewHolder.tvRankName.setTextColor(textColor);
        rankViewHolder.tvRankDistance.setTextColor(textColor);
        rankViewHolder.tvRankStep.setTextColor(textColor);
        rankViewHolder.tvRankPace.setTextColor(textColor);
        rankViewHolder.tvRankDuration.setTextColor(textColor);
        rankViewHolder.tvRankHeart.setTextColor(textColor);
        rankViewHolder.tvRankName.setText(rankData.getNickname());
        double distance=rankData.getDistance();
        int pace=rankData.getPace();
        int minute=pace%256;
        int second=pace/256;
        NumberFormat numberFormat= ResolveData.getNumberFormat(2);
        rankViewHolder.tvRankDistance.setText(numberFormat.format(distance)+" km");
        rankViewHolder.tvRankStep.setText(String.valueOf(rankData.getDays())+context.getResources().getString(R.string.day));
        rankViewHolder.tvRankPace.setText(DateUtil.getPaceTime(minute*60+second));
        rankViewHolder.tvRankDuration.setText(DateUtil.getRankTime(rankData.getActiveTime(),context));
        rankViewHolder.tvRankHeart.setText(rankData.getLike()+"");
        Glide.with(context)
                .load(rankData.getAvatar()).dontAnimate()
                .error(context.getResources().getDrawable(R.drawable.rank_top))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(rankViewHolder.ivRankIcon);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,GrowthMianRecordActivity.class);
                intent.putExtra("uid",rankData.getUid()+"");//my代表自己查看的数据
                intent.putExtra("uname",rankData.getNickname());//my代表自己查看的数据
                context.startActivity(intent);
            }
        });

        //是否已经点赞
        if(rankData.getStatus()==1){
            setDrawable(rankViewHolder.tvRankHeart,R.drawable.rankred);
        }else{
            setDrawable(rankViewHolder.tvRankHeart,R.drawable.rank_heart);
        }
        rankViewHolder.tvRankHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rankData.getStatus()==1){//取消点赞
                    rankData.setStatus(0);
                    rankData.setLike(rankData.getLike()-1);
                    setDrawable(rankViewHolder.tvRankHeart,R.drawable.rank_heart);
                    rankViewHolder.tvRankHeart.setText(rankData.getLike()+"");
                    mGoodView.setText("-1");
                    mGoodView.setTextColors(Color.MAGENTA);
                    mGoodView.show(rankViewHolder.tvRankHeart);
                    Liked(rankData);
                }else{
                    rankData.setStatus(1);
                    rankData.setLike(rankData.getLike()+1);
                    setDrawable(rankViewHolder.tvRankHeart,R.drawable.rankred);
                    rankViewHolder.tvRankHeart.setText(rankData.getLike()+"");
                    mGoodView.setText("+1");
                    mGoodView.setTextColors(Color.RED);
                    mGoodView.show(rankViewHolder.tvRankHeart);
                    Liked(rankData);
                }
            }
        });

    }
    private   void Liked(RankData.DataBean tt){
        Map<String, Object> map = new HashMap<>();
        map.put("uid", NetWorkUtil.getUserId());
        map.put("likedUid", tt.getUid()+"");
        map.put("actId", getActId);
        NetWorkUtil.getInstance().getJstyleApi().huodongliked(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
            @Override
            public void onSubscribe(Disposable d) { }
            @Override
            public void onNext(Activitvtiesmsg activitvtiesData) { }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });
    }

private void setDrawable(TextView tv,int src){
    Drawable rightDrawable = context.getResources().getDrawable(src);
    rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());  // left, top, right, bottom
    tv.setCompoundDrawables(null, null, null, rightDrawable);
}
    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new RankViewHolder( LayoutInflater.from(context).inflate(R.layout.item_rankactivity, viewGroup, false));
    }


    @Override
    public int getItemCount() {
        return null==mDataList?0:mDataList.size();
    }
    class RankViewHolder extends BaseViewHolder{
        @BindView(R.id.tv_rank_level)
        TextView tvRankLevel;
        @BindView(R.id.iv_rank_icon)
        ImageView ivRankIcon;
        @BindView(R.id.tv_rank_name)
        TextView tvRankName;
        @BindView(R.id.tv_rank_distance)
        TextView tvRankDistance;
        @BindView(R.id.tv_rank_step)
        TextView tvRankStep;
        @BindView(R.id.tv_rank_pace)
        TextView tvRankPace;
        @BindView(R.id.tv_rank_duration)
        TextView tvRankDuration;
        @BindView(R.id.tv_rank_heart)
        TextView tvRankHeart;
         RankViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
